<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class ReserveController extends HomebaseController {
	public function clear(){
		$_POST=null;
		session("reservewhere['lid']",null);
		session("reservewhere['uid']",null);
		session("reservewhere['status']",null);
		$this->showReserve();
	}
	public function showReserve(){
		$Reserve=M('reserve');
		$Lesson=M('lesson');
		$Member=M('member');
		session(array('name'=>'session_id','expire'=>3600));
		//lid
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
			session("reservewhere['lid']",$where['lid']);
		}else if(!$_POST['lid'] && session("reservewhere['lid']")){
			$where['lid']=session("reservewhere['lid']");
		}
		//uid
		if($_POST['uid']){
			$where['uid']=$_POST['uid'];
			session("reservewhere['uid']",$where['uid']);
		}else if(!$_POST['uid'] && session("reservewhere['uid']")){
			$where['uid']=session("reservewhere['uid']");
		}
		//status
		if($_POST['status']){
			if($_POST['status']==99){
				$ww['status']=99;
				session("reservewhere['status']",null);
			}else{
				$where['status']=$_POST['status'];
				session("reservewhere['status']",$where['status']);
			}
		}else if(!$_POST['status'] && session("reservewhere['status']")){
			$where['status']=session("reservewhere['status']");
		}
		$ww=$where;
		$count=$Reserve->where($where)->count();
		$page=$this->page($count,10);
		$reserve=$Reserve->where($where)
		->order('ordertime desc')
		->limit($page->firstRow , $page->listRows)
		->select();
		for($i=0;$i<count($reserve);$i++){
			//课程详情
			$where['lid']=$reserve[$i]['lid'];
			$le[$i]=$Lesson->where($where)->find();
			//$re[$i]['ordertime']=strval($re[$i]['ordertime']);
			$reserve[$i]['ordertime']=substr($reserve[$i]['ordertime'],0,10);
			$reserve[$i]['le']=$le[$i];
			//用户信息
			$where2['uid']=$reserve[$i]['uid'];
			$member[$i]=$Member->where($where2)->find();
			$reserve[$i]['name']=$member[$i]['name'];
			$reserve[$i]['tel']=$member[$i]['tel'];
		}
		$this->assign('re',$reserve)
		->assign('where',$ww)
		->assign("page", $page->show('Admin'));
		$this->display(':showReserve');
	}
	/*public function showReserve2(){
		$Reserve=M('reserve');
		$Lesson=M('lesson');
		$Member=M('member');
		$where['status']=2;
		$reserve=$Reserve->where($where)->order('ordertime')->select();
		//dump($reserve);
		//die();
		for($i=0;$i<count($reserve);$i++){
			//课程详情
			$where['lid']=$reserve[$i]['lid'];
			$le[$i]=$Lesson->where($where)->find();
			//$re[$i]['ordertime']=strval($re[$i]['ordertime']);
			$reserve[$i]['ordertime']=substr($reserve[$i]['ordertime'],0,10);
			$reserve[$i]['le']=$le[$i];
			//用户信息
			$where2['uid']=$reserve[$i]['uid'];
			$member[$i]=$Member->where($where2)->find();
			$reserve[$i]['name']=$member[$i]['name'];
			$reserve[$i]['tel']=$member[$i]['tel'];
		}
		$this->assign('re',$reserve)
		->assign("page", $page->show('Admin'));
		$this->display(':showReserve');
	}*/
	public function addReserve(){
		$this->display(':addReserve');
	}
	public function commit(){
		$Reserve=M('reserve');
		$Lesson=M('lesson');
		$Member=M('member');
		//开启事务
		$wh['lid']=$_POST['lid'];
		$uplimit=$Lesson->where($wh)->getField('uplimit');
		$ordered=$Lesson->where($wh)->getField('ordered');
		$left=$uplimit-$ordered;
		if($left>0){
			$Member->startTrans();
			if($_POST['uid']){
				$data2['uid']=$_POST['uid'];
			}else{
				$data1['name']=$_POST['name'];
				$data1['tel']=$_POST['tel'];
				//1.新增用户
				$data2['uid']=$Member->add($data1);
			}
			$data2['status']=1;
			$data2['lid']=$wh['lid'];
			$data2['ordertime']=date('Y-m-d h:i:s');
			//2.新增订单
			$reid=$Reserve->add($data2);
			//3.预约人数+1
			$msg2=$Lesson->where($wh)->setInc('ordered');
			if($data2['uid'] && $reid  && $msg2){
				$Member->commit();//提交事务
				echo "<script>alert('预约成功！');history.go(-1);</script>";
			}else{
				$Member->rollback();//事务回滚
				echo "<script>alert('预约失败！');history.go(-1);</script>";
			}
		}else{
			echo "<script>alert('预约失败！');history.go(-1);</script>";
		}
	}
	public function showRhand(){
		$Reserve=M('reserve');
		$Lesson=M('lesson');
		$Member=M('member');
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
		}
		if($_POST['uid']){
			$where['uid']=$_POST['uid'];
		}
		if($_POST['status']){
			$where['status']=$_POST['status'];
		}else{
			$where['status']=1;
		}
		$reserve=$Reserve->where($where)->order('ordertime')->select();
		//dump($reserve);
		//die();
		$c=count($reserve);
		for($i=0;$i<$c;$i++){
			//课程详情
			$where['lid']=$reserve[$i]['lid'];
			$le[$i]=$Lesson->where($where)->find();
			//$re[$i]['ordertime']=strval($re[$i]['ordertime']);
			$reserve[$i]['ordertime']=substr($reserve[$i]['ordertime'],0,10);
			$reserve[$i]['le']=$le[$i];
			//用户信息
			$where2['uid']=$reserve[$i]['uid'];
			$member[$i]=$Member->where($where2)->find();
			$reserve[$i]['name']=$member[$i]['name'];
			$reserve[$i]['tel']=$member[$i]['tel'];
			$reserve[$i]['openid']=$member[$i]['openid'];
		}
		for($i=0;$i<$c;$i++){
			if(empty($reserve[$i]['openid'])){
				$rH[$i]=$reserve[$i];
			}
		}
		$this->assign('re',$rH);
		$this->display(":showRhand");
	}
	public function cancel(){
		$where['lid']=$lid=$_GET['lid'];
		$msg1=$this->Lesson->where($where)->select();
		//判断日期
		$d=str_replace('-','',$msg1[0]['sdate'])-date('Ymd',time());//大于0则可以取消，小于0不能取消，等于0则判断可否取消
		$h=str_replace(':','',substr($msg1[0]['stime'],0,5))-date('Hi');
		//小于100则在开课1小时以内
		if($d<0){
			echo "<script>alert('课程已结束,不能取消！');location.href='index.php?m=Choose&a=order_complete';</script>";
		}elseif($d==0&&$h<=100){
			echo"<script>alert('开课前1小时不能取消！');location.href='index.php?m=Choose&a=order_complete';</script>";
		}else{
			$where['uid']=$_GET['uid'];//需要获取用户uid,此时条件为where['lid']和where['uid']
			$status=$this->Reserve->where($where)->getField('status');
			//判断订单status
			if($status==2){
				//排队中的订单，直接删除
				$this->Reserve->startTrans();
				$msg2=$this->Reserve->where($where)->delete();
				if($msg2){
					$this->Reserve->commit();
					echo "<script>alert('取消成功！');location.href='index.php?m=Choose&a=order_complete';</script>";
				}else{
					$this->Reserve->rollback();
					echo "<script>alert('取消失败，请稍后再试！');location.href='index.php?m=Choose&a=order_complete';</script>";
				}
			}elseif($status==1){
				//删除预约成功的订单
				$this->Reserve->startTrans();
				$msg2=$this->Reserve->where($where)->delete();
				//判断删除是否成功
				if($msg2){
					//根据lid取出和status取出排队的人
					$where2['lid']=$where['lid'];
					$where2['status']=2;
					$wait=$this->Reserve->where($where2)->order('ordertime')->limit(1)->select();
					//第一个排队的人
					if($wait){
						//若有人在排队，则更改第一个排队订单的status=1
						$where3['reid']=$wait[0]['reid'];
						$waitsave=$this->Reserve->where($where3)->setField('status','1');
						if($waitsave){
							$msgreid=$wait[0]['reid'];
							$this->sendmsg($msgreid);
							$this->Reserve->commit();
							echo "<script>alert('取消成功！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}else{
							$this->Reserve->rollback();
							echo "<script>alert('取消失败，请稍后再试！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}
					}else{
						//若没人排队
						$m['ordered']=$msg1[0]['ordered']-1;
						$msg3=$this->Lesson->where("lid='$lid'")->save($m);//ordered已订人数-1
						if($msg3){
							$this->Reserve->commit();
							echo "<script>alert('取消成功！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}else{
							$this->Reserve->rollback();
							echo "<script>alert('取消失败，请稍后再试！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}
					}
					
				}else{
					$this->Reserve->rollback();
					echo "<script>alert('取消失败，请稍后再试！');location.href='index.php?m=Choose&a=order_complete';</script>";
				}
			}
		}
	}
}